package com.minyifang.wcspf.work.ui.home.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.home.view.GoodsListView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class GoodsListPresenter extends BasePresenter<GoodsListView> {
    public void getGoodsList(String comClass, int page) {
//        {"storeId":2,"comClass":"b731e50a657011e88990d4bed9ae62f6,0","page":0,"limit":2}
        clearRequest();
        Map<String, Object> data = new HashMap<>();
        data.put("comClass", comClass);
        data.put("page", page);
        data.put("limit", 10);
        if (page == 0)
            getView().showLoadingView();
        final JSONObject jsonData = Constants.getJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getGoodsListByClass(requestBody), listHttpRespond -> {
            getView().hideLoadingView();
            if (listHttpRespond.result == 1) {
                getView().onGetListSucceed(listHttpRespond.data);
            }
        });
    }

    public void collectionGoods(String goodsId, int type) {
        Map<String, Object> data = new HashMap<>();
        data.put("comId", goodsId);
        data.put("type", type);
        JSONObject aesJsonData = Constants.getAESJsonData(data, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().collectionGoods(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1)
                getView().showCollectionResult(respond.message);
            else
                getView().onRequestFailed(respond.message);
        });
    }

    public void addToCar(String skuId, String quantity) {
        Map<String, Object> map = new HashMap<>();
        map.put("skuId", skuId);
        map.put("quantity", quantity);
        JSONObject jsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().addToCar(requestBody), stringHttpRespond -> getView().showAddToCarRes(stringHttpRespond));
    }
}
